﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TierApplication;
using TierService;
using DevExpress.XtraTreeList.Nodes;
using TierApplication;


namespace TierUsrCtl
{
    public partial class ucUser : UserControl
    {
        public delegate void PopUp(object sender, CancelEventArgs e);
        public event PopUp OnPopUp;

        private string _UserID = "";
        private string _UserName = "";
        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        public string UserID
        {
            get { return _UserID; }
            set
            {
                _UserID = value;
                if ((_UserID != "") && (_UserID != null))
                {
                    P_User mUser = new P_User();
                    mUser.GetModel(_UserID);
                    _UserName = mUser.UserName;
                    txtUserID.Tag = _UserID;
                    txtUserID.Text = _UserName;
                }
                else
                {
                    txtUserID.Tag = "";
                    txtUserID.EditValue = "";
                    txtUserID.Text = "";
                }
            }
        }


        public ucUser()
        {
            InitializeComponent();
            LayoutControls();
            this.Width = 110;
            this.popupCtlApparatus.PopupContainerProperties.CloseOnLostFocus = false;
            this.popupCtlApparatus.PopupContainerProperties.CloseOnOuterMouseClick = false;
            this.BackColorChanged += new EventHandler(ucApparatus_BackColorChanged);
        }

        void ucApparatus_BackColorChanged(object sender, EventArgs e)
        {
            txtUserID.BackColor = this.BackColor;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.LayoutControls();
        }

        private void LayoutControls()
        {
            this.txtUserID.Width = this.Width;
            this.Height = this.txtUserID.Height;
            //popupCtlApparatus.Width = 400;
            popupCtlApparatus.Height = 400;
            //this.txtID.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            this.txtUserID.Properties.ShowPopupCloseButton = true;

            //T_SpecimenBox mBox = new T_SpecimenBox();
            //Utility.DataToLookupComboxEx(txtBoxID, mBox.GetList(""), "BoxID", "BoxName");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sWhere = "";
          

            if (txtUserName.Text != "")
            {
                sWhere = sWhere + " and UserName like '%" + txtUserName.Text + "%'";
            }
            //if (ckCommanUse.Checked)
            //    sWhere = sWhere + " and  Exists (select 1 from T_SpecimenApparatusCommonUse where ApparatusID=a.ApparatusID and DeptID='" + TierService.Utility.gAdminDeptID + "')";
            
            string sUserID =TierService.Utility.gUserID;
            P_User mUser = new P_User();
            if (TierService.Utility.gSeeAllUser)
                sUserID = "0";
            DataSet ds = mUser.GetUserTreeList(sUserID);
            tlUser.DataSource = ds.Tables[0].DefaultView;

            //LocateApparatusID(ApparatusID);
        }

        private void txtID_QueryPopUp(object sender, CancelEventArgs e)
        {
            try
            {
                OnPopUp(sender, e);
            }
            catch (Exception)
            {
            }

            btnSearch_Click(null, null);
            //LocateApparatusID(ApparatusID);
        }

        private void LocateApparatusID(string sApparatusID)
        {
            //if (sApparatusID != "")
            //{
            //    for (int i = 0; i < this.gvApparatus.RowCount; i++)
            //    {
            //        string CellText = this.gvApparatus.GetRowCellDisplayText(i, "ApparatusID");
            //        if (CellText == sApparatusID)
            //        {
            //            this.gvApparatus.FocusedRowHandle = i;
            //            break;
            //        }
            //    }
            //}
        }
        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Delete) || (e.KeyCode == Keys.Back))
            {
                txtUserID.Tag = "";
                txtUserID.EditValue = "";
                txtUserID.Text = "";
                UserID = "";
                UserName = "";
            }
        }

        private void gvApparatus_DoubleClick(object sender, EventArgs e)
        {
            //if (gvApparatus.RowCount < 1) return;
            //DataRow dr = gvApparatus.GetDataRow(gvApparatus.FocusedRowHandle);
            //string sApparatusID = dr["ApparatusID"].ToString();
            //ApparatusID = sApparatusID;
            txtUserID.ClosePopup();
        }

        private void tlApparatus_DoubleClick(object sender, EventArgs e)
        {
            TreeListNode node = tlUser.FocusedNode;
            //if (node.HasChildren)
            //{
            //    return;
            //}
            P_User mUser = new P_User();
            string sApparatusID = node["UserID"].ToString();
            UserID = sApparatusID;
            txtUserID.ClosePopup();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {

        }
    }
}
